const wxpay = require('../../utils/pay.js')
const app = getApp()
const WXAPI = require('../../wxapi/main')
Page({
  data: {
    statusType: [{val: "", name: "全部" }
      , {val: 0, name: "待付款" }
      , {val: 1, name: "未使用" }
      , {val: 99, name: "已使用" }],
    hasRefund: false,
    currentType: "",
    currentIndex: 0,
    tabClass: ["", "", "", "", ""]
  },
  statusTap: function(e) {
    const status = this.data.statusType[e.currentTarget.dataset.index];
    const curType = status.val;
    this.data.currentType = curType.id;
    this.setData({
      currentType: curType,
      currentIndex:e.currentTarget.dataset.index
    });
    this.onShow();
  },
  cancelOrderTap: function(e) {
    const that = this;
    const orderId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确定要取消该订单吗？',
      content: '',
      success: function(res) {
        if (res.confirm) {
          WXAPI.orderClose(orderId).then(function(res) {
            if (res.type == 'success') {
              that.onShow();
            }
          })
        }
      }
    })
  },
  shuohuoOrderTap: function (e) {
    const that = this;
    const orderId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确定收货吗？',
      content: '',
      success: function (res) {
        if (res.confirm) {
          WXAPI.ordershouhuo(orderId).then(function (res) {
            if (res.type == 'success') {
              that.onShow();
            }
          })
        }
      }
    })
  },
  refundApply (e) {
    // 申请售后
    const orderId = e.currentTarget.dataset.id;
    const amount = e.currentTarget.dataset.amount;
    wx.navigateTo({
      url: "/pages/order/refundApply?id=" + orderId + "&amount=" + amount
    })
  },
  toPayTap: function(e) {
    const that = this;
    const orderId = e.currentTarget.dataset.id;
    let money = e.currentTarget.dataset.money;
    const needScore = e.currentTarget.dataset.score;
    WXAPI.userZfxx(orderId).then(function(res) {
      if (res.type == 'success') {
        let _msg = '订单金额: ' + money +' 元';
        let type = 1;
        let _useryue = res.resultData.row;
        if (_useryue.dbyue >= money) {
          _msg += ',可用代金券余额为 ' + _useryue.dbyue +' 元';
          type = 3; 
        } else if (_useryue.yue >= money){
          _msg += ',可用余额为 ' + _useryue.yue + ' 元'  
          type = 2;     
        }
        money = money
        wx.showModal({
          title: '请确认支付',
          content: _msg,
          confirmText: "确认支付",
          cancelText: "取消支付",
          success: function (res) {
            if (res.confirm) {
              that._toPayTap(orderId, money, type)
            } else {
              console.log('用户点击取消支付')
            }
          }
        });
      } else {
        wx.showModal({
          title: '错误',
          content: '无法获取用户资金信息',
          showCancel: false
        })
      }
    })
  },
  _toPayTap: function (orderId, money,type){
    const _this = this
    if (type > 1) {
      // 直接使用余额支付
      var data = { id: orderId, money: money, type: type};
      WXAPI.orderPay(data).then(function (res) {
        _this.onShow();
      })
    } else {
      wxpay.wxpay('order', money, orderId, "/pages/order-list/index");
    }
  },
  onLoad: function(options) {
    if (options && options.type) {
      if (options.type == 4) {
        this.setData({
          hasRefund: true,
          currentType: options.type
        });
      } else {
        const status = this.data.statusType[options.type];
        const curType = status.val;
        this.data.currentType = curType.id;

        this.setData({
          hasRefund: false,
          currentType: curType,
          currentIndex: options.type
        });
      }      
    }
    this.onShow();
  },
  onReady: function() {
    // 生命周期函数--监听页面初次渲染完成

  },
  getOrderStatistics: function() {
    var that = this;
    var tabClass = that.data.tabClass;
    tabClass[0] = "red-dot";
    tabClass[1] = "red-dot";
    tabClass[2] = "red-dot";
    tabClass[3] = "red-dot";
    that.setData({
      tabClass: tabClass,
    });
  },
  onShow: function() {
    // 获取订单列表
    var that = this;
    var postData = {};
    postData.status = that.data.currentType;
    this.getOrderStatistics();
    WXAPI.orderList(postData).then(function(res) {
      if (res.type == 'success') {
        that.setData({
          orderList: res.resultData.rows
        });
      } else {
        that.setData({
          orderList: null
        });
      }
    })
  },
  onHide: function() {
    // 生命周期函数--监听页面隐藏

  },
  onUnload: function() {
    // 生命周期函数--监听页面卸载

  },
  onPullDownRefresh: function() {
    // 页面相关事件处理函数--监听用户下拉动作

  },
  onReachBottom: function() {
    // 页面上拉触底事件的处理函数

  }
})